<?php
/**
 * Created by PhpStorm.
 * User: HP
 * Date: 2018/5/8
 * Time: 16:53
 */
namespace app\data;

use app\common\Common;
use think\Db;

class UserData extends Base
{
    /**
     * 添加用户
     * @param $user_name
     * @param $user_head_img
     * @param $user_sex
     * @param $user_open_id
     * @param $union_id
     * @param $province
     * @param $city
     * @return array
     */
    public function addUser($user_name, $user_head_img, $user_sex, $user_open_id, $union_id, $province, $city)
    {
        $time = time();
        $id = Db::table('drh_user_info')
            ->insertGetId([
                'user_name' => $user_name,
                'user_head_img' => $user_head_img,
                'user_sex' => $user_sex,
                'user_open_id' => $user_open_id,
                'union_id' => $union_id,
                'province' => $province,
                'city' => $city,
                'create_time' => $time,
            ]);
        session($user_open_id, $id);
        return ['code' => $this->t_code, 'msg' => '添加成功', 'data' => $id];
    }

    public function addUserCode($code, $user_id)
    {
        return Db::table('drh_user_info')
            ->where([
                'id' => $user_id
            ])->update([
                'user_code' => $code
            ]);
    }


    /**
     * @param $user_open_id
     * @param $user_name
     * @param $user_head_img
     * @param $user_sex
     * @return array
     */
    public function editUser($user_open_id, $user_name, $user_head_img, $user_sex)
    {
        Db::table('drh_user_info')
            ->where([
                'user_open_id' => $user_open_id
            ])->update([
                'user_name' => $user_name,
                'user_head_img' => $user_head_img,
                'user_sex' => $user_sex,
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    public function getAddressCount($user_id)
    {
        return Db::table('drh_delivery_address')
            ->field([
                'count(*) count',
            ])->where([
                'user_id' => $user_id
            ])
            ->group('user_id')
            ->find();
    }

    public function addAddress($user_id, $user_name, $user_tel, $delivery_address, $zip_code,
                               $address_province, $address_municipal, $address_district, $province_id, $municipal_id, $district_id)
    {
        $time = time();
        Db::table('drh_delivery_address')
            ->insert([
                'user_id' => $user_id,
                'delivery_user_name' => $user_name,
                'delivery_user_tel' => $user_tel,
                'address_province' => $address_province,
                'province_id' => $province_id,
                'address_municipal' => $address_municipal,
                'municipal_id' => $municipal_id,
                'address_district' => $address_district,
                'district_id' => $district_id,
                'delivery_address' => $delivery_address,
                'zip_code' => $zip_code,
                'create_time' => $time,
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    public function editAddress($id, $user_id, $user_name, $user_tel, $delivery_address, $zip_code,
                                $address_province, $address_municipal, $address_district,
                                $province_id, $municipal_id, $district_id)
    {
        Db::table('drh_delivery_address')
            ->where([
                'user_id' => $user_id,
                'id' => $id
            ])
            ->update([
                'delivery_user_name' => $user_name,
                'delivery_user_tel' => $user_tel,
                'address_province' => $address_province,
                'province_id' => $province_id,
                'address_municipal' => $address_municipal,
                'municipal_id' => $municipal_id,
                'address_district' => $address_district,
                'district_id' => $district_id,
                'delivery_address' => $delivery_address,
                'zip_code' => $zip_code,
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    public function showAddress($user_id, $id = 0)
    {
        if ($id == 0) {
            return Db::table('drh_delivery_address')
                ->where([
                    'user_id' => $user_id
                ])->select();
        } else {
            return Db::table('drh_delivery_address')
                ->where([
                    'user_id' => $user_id,
                    'id' => $id
                ])->find();
        }
    }

    public function addDefaultAddress($id, $user_id)
    {
        return Db::table('drh_user_info')
            ->where([
                'id' => $user_id
            ])->update([
                'is_default_addr' => $id
            ]);
    }

    public function getDefaultAddress($user_id)
    {
        $res = Db::table('drh_user_info')
            ->field([
                'is_default_addr'
            ])->where([
                'id' => $user_id
            ])->find();
        return $res['is_default_addr'];
    }

    public function delAddress($id, $user_id)
    {
        Db::table('drh_delivery_address')
            ->where([
                'id' => $id,
                'user_id' => $user_id
            ])->delete();
        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }


    /**
     * 获取用户信息
     * @param $user_open_id
     * @param array $value
     * @param string $whereName
     * @param string $table_name
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getInfo($user_open_id, array $value, $whereName = 'user_open_id', $table_name = 'drh_user_info')
    {
        $value = implode(',', $value);
        return Db::table($table_name)
            ->field($value)
            ->where([
                $whereName => $user_open_id
            ])->find();
    }

    public function integralToMoney($integral, $userInfo)
    {
        $user_id = $userInfo['id'];
        $user_integral_start = $userInfo['user_integral'];//用户初始积分
        $user_money_start = $userInfo['user_money'];//用户初始金钱 f
        $integral_ratio
            =
            empty(session('integral_ratio')) ? Common::getConfig('integral_ratio') : session('integral_ratio');//积分金钱比例

        $money = ($integral / $integral_ratio) * 100;
        $user_money = $user_money_start + $money;//用户剩余金钱
        $user_integral = $user_integral_start - $integral;//用户剩余积分
        $time = time();
        $order_num = Common::getOrderNumber('l', 10, $time);//单号
        Db::startTrans();
        try {
            $this->addIntegralMoney($user_id, $integral, $time, $money, $order_num);
            $this->addUserFlow($user_id, $user_integral_start, $user_money_start, $time, $money, '-' . $integral,
                $order_num, 2, $user_money_start + $money, $user_integral_start - $integral);
            $this->changIntegralMoney($user_id, $user_integral, $user_money);
            Db::commit();
            return ['code' => $this->t_code, 'msg' => '转换成功'];
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => $this->f_code, 'msg' => $e->getMessage()];
        }
    }

    public function changIntegralMoney($user_id, $user_integral, $user_money)
    {
        Db::table('drh_user_info')
            ->where([
                'id' => $user_id
            ])->update([
                'user_integral' => $user_integral,
                'user_money' => $user_money,
            ]);
        return true;
    }

    /**
     * 添加转换表
     * @param $user_id
     * @param $integral
     * @param $time
     * @param $money
     * @param $order_num
     * @return bool
     */
    public function addIntegralMoney($user_id, $integral, $time, $money, $order_num)
    {

        Db::table('drh_integral_money')
            ->insert([
                'order_num' => $order_num,
                'user_id' => $user_id,
                'integral' => $integral,
                'money' => $money,
                'create_time' => $time,
            ]);
        return true;
    }

    /**
     * 添加流水表
     * @param $user_id
     * @param $user_integral
     * @param $user_money
     * @param $time
     * @param $money_expenses
     * @param integral_expenses
     * @param $order_num
     * @param $type_status
     * @param $end_money
     * @param $end_integral
     * @param $fees
     * @return bool
     */
    public function addUserFlow($user_id, $user_integral, $user_money, $time, $money_expenses, $integral_expenses,
                                $order_num, $type_status, $end_money, $end_integral,$return_integral, $fees = 0)
    {
        Db::table('drh_user_flow')
            ->insert([
                'user_id' => $user_id,
                'start_money' => $user_money,
                'start_integral' => $user_integral,
                'order_num' => $order_num,
                'type_status' => $type_status,
                'money_expenses' => $money_expenses,
                'integral_expenses' => $integral_expenses,
                'end_money' => $end_money,
                'end_integral' => $end_integral+$return_integral,
                'return_integral'=>$return_integral,
                'create_time' => $time,
                'fees' => $fees
            ]);
    }

    public function withdraw($money, $userInfo)
    {

    }

    /**
     * 添加提现
     * @param $user_id
     * @param $order_num
     * @param $total_money
     * @param $time
     * @param $money
     * @param $fees
     * @param $fees_proportion
     * @return bool
     */
    public function addWithdraw($user_id, $order_num, $total_money, $time, $money, $fees, $fees_proportion)
    {
        Db::table('drh_withdraw')
            ->insert([
                'user_id' => $user_id,
                'order_num' => $order_num,
                'total_money' => $total_money,
                'money' => $money,
                'fees' => $fees,
                'create_time' => $time,
                'fees_proportion' => $fees_proportion,
                'review_type' => 2
            ]);
        return true;
    }

    /**
     * 添加分销
     * @param $user_id
     * @param $id
     * @return array
     */
    public function addDistributionUser($user_id, $id)
    {
        $time = time();
        Db::table('drh_user_friend')
            ->insert([
                'user_id' => $user_id,
                'friend_id' => $id,
                'create_time' => $time,
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    public function createBargainOrder($goods_id, $shop_id, $bargain_info, $user_id, $goods_attr)
    {
        $time = time();
        $id = Db::table('drh_user_goods_bargain')
            ->insertGetId([
                'goods_id' => $goods_id,
                'shop_id' => $shop_id,
                'user_id' => $user_id,
                'goods_attr' => $goods_attr,
                'bargain_total_money' => $bargain_info['bargain_total_money'],
                'bargain_once_money' => $bargain_info['bargain_once_money'],
                'title' => $bargain_info['title'],
                'bargain_num' => ceil($bargain_info['bargain_total_money'] / $bargain_info['bargain_once_money']),
                'create_time' => $time,
            ]);
        return ['code' => $this->t_code, 'msg' => '砍价设置成功',
            'data' => [
                'url' => config('url') . 'show/goods/bargainInfo?id=' . $id,
                'msg' => $bargain_info['title'],
                'id'=>$id
            ]];
    }

    public function getBargainGoods($id)
    {
        return Db::table('drh_user_goods_bargain')
            ->field([
                'bargain_open_id',
                'bargain_num',
                'bargain_total_num',
                'bargain_once_money'
            ])->where([
                'id' => $id
            ])->find();
    }

    public function editBargainOrder($id, $bargain_num, $bargain_open_id, $bargain_total_num,$bargain_once_money)
    {
        Db::table('drh_user_goods_bargain')
            ->where([
                'id' => $id
            ])->update([
                'bargain_num' => $bargain_num,
                'bargain_open_id' => $bargain_open_id,
                'bargain_total_num' => $bargain_total_num,
            ]);
        return ['code' => $this->t_code, 'msg' => '砍价成功','data'=>['money'=>$bargain_once_money]];
    }

    public function getBargainGoodInfo($id)
    {

    }

    /**
     * 获取分销提成比例
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getDistributionRule()
    {
        return Db::table('drh_distribution_rule')
            ->field([
                'lv',
                'proportion',
            ])->select();
    }

    public function getDistributionUser($user_id)
    {
        return Db::table('drh_user_friend a')
            ->join('drh_user_friend b', 'a.friend_id=b.user_id', 'left')
            ->field([
                'a.user_id parent_id',
                'b.user_id id'
            ])->where([
                'b.friend_id' => $user_id
            ])->select();
    }

    public function addIntegral($user_id, $give_integral)
    {
        Db::table('drh_user_info')
            ->where([
                'id' => $user_id
            ])->setInc('user_integral', $give_integral);
        return true;
    }

    public function endOrder($id, $money, $integral)
    {

        Db::table('drh_user_info')
            ->where([
                'id' => $id
            ])->update([
                'user_money' => $money,
                'user_integral' => $integral
            ]);

        return true;
    }

    public function commentGoods($user_id, $order_num, $goods_id, $lv, $msg)
    {
        $time = time();
        Db::table('drh_goods_comment')
            ->insert([
                'user_id' => $user_id,
                'goods_id' => $goods_id,
                'order_num' => $order_num,
                'lv' => $lv,
                'msg' => $msg,
                'create_time' => $time
            ]);
        return ['code' => $this->t_code, 'msg' => '打分成功'];
    }

    public function showUserInfo($where)
    {
        $res = Db::table('drh_user_info')
            ->field([
                'id',
                'user_name',
                'user_head_img',
                'user_sex',
                'user_tel',
                'user_integral',
                'user_money'
            ])->where($where)->find();
        return ['code' => $this->t_code, 'msg' => 'OK', 'data' => $res];
    }

    public function getWithdrawMoney($user_id)
    {
        $res= Db::table('drh_user_flow')
            ->field([
                'sum(money_expenses) withdraw_money'
            ])->where([
                'user_id'=>$user_id,
                'type_status'=>3
            ])->find();
        return empty($res['withdraw_money'])?0:$res['withdraw_money'];
    }

    public function getIncomeMoney($user_id,$time_start,$time_end)
    {
        $res= Db::table('drh_user_flow')
            ->field([
                'sum(money_expenses) income_money'
            ])->where("user_id={$user_id} and money_expenses > 0 
            and create_time >{$time_start} and create_time < {$time_end}")->find();
        return empty($res['income_money'])?0:$res['income_money'];
    }

    public function showMemberPR($id)
    {
        return Db::table('drh_user_friend a')
            ->join('drh_user_friend b', 'a.friend_id=b.user_id', 'left')
            ->field([
                'b.friend self_id',
                "IFNULL(a.user_id,0) pre_one_id",
                "IFNULL(b.user_id ,0)pre_two_id"
            ])->where([
                'b.friend_id' => $id
            ])->find();
    }

    public function showMemberNR($id)
    {
        return Db::table('drh_user_friend a')
            ->join('drh_user_friend b', 'a.friend_id=b.user_id', 'left')
            ->field([
                'a.user_id self_id',
                "IFNULL(b.user_id ,0) next_one_id",
                "IFNULL(c.friend_id ,0) next_two_id"
            ])->where([
                'a.user_id' => $id
            ])->find();
    }

////////////////////////////////////////////-----代理-----\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


    public function addProxyMoney($id, $money)
    {
        Db::table('drh_proxy')
            ->where([
                'area_id' => $id,
            ])->update("total_money = total_money + { $money }, money = money + {$money}");
        return true;
    }

    public function addProxyFlow($user_id, $start_money, $type_status, $money_expenses, $fees, $end_money, $msg, $time)
    {
        Db::table('drh_proxy_user_flow')
            ->insert([
                'user_id' => $user_id,
                'start_money' => $start_money,
                'type_status' => $type_status,
                'money_expenses' => $money_expenses,
                'fees' => $fees,
                'end_money' => $end_money,
                'msg' => $msg,
                'time' => $time,
            ]);
        return true;
    }

    public function searchProxy($user_tel)
    {
        return Db::table('drh_user_info a')
            ->join('drh_proxy b', 'a.id=b.user_id', 'left')
            ->field([
                'a.id',
                'b.type_lv',
                'b.area_id',
                'b.recover_status'
            ])->where([
                'a.user_tel' => $user_tel
            ])->find();
    }

    public function addShoppingCart($user_id, $goods_id, $time, $goods_attr_value, $goods_num)
    {
        Db::table('drh_user_shopping_cart')
            ->insert([
                'user_id' => $user_id,
                'goods_id' => $goods_id,
                'create_time' => $time,
                'goods_attr_value' => $goods_attr_value,
                'goods_num' => $goods_num
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    public function showShoppingCart($user_id, $page, $pre)
    {
        $page = ($page - 1) * $pre;
        return Db::table('drh_user_shopping_cart a')
            ->join('drh_goods b', 'a.goods_id=b.id', 'left')
            ->join('drh_shop c', 'b.shop_id=c.id', 'left')
            ->field([
                'a.id',
                'a.goods_num',
                'b.shop_id',
                'a.goods_id',
                'b.goods_name',
                'b.goods_price',
                'b.give_integral',
                'b.goods_stock',
                'b.goods_img',
                'a.goods_attr_value',
                "b.goods_price*a.goods_num  total_money",
                'b.goods_attr_value goods_attr_values',
                'c.shop_name',
            ])->where([
                'a.user_id' => $user_id
            ])->order("a.create_time desc ")
            ->limit($page, $pre)
            ->select();
    }

    public function delShoppingCart($id, $user_id, $goods_id)
    {
        Db::table('drh_user_shopping_cart')
            ->where([
                'user_id' => $user_id,
                'goods_id' => $goods_id,
                'id' => $id,
            ])->delete();
        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    public function delShoppingCartByPay($goods_attr_value,$goods_id,$user_id)
    {
        Db::table('drh_user_shopping_cart')
            ->where([
                'user_id' => $user_id,
                'goods_id' => $goods_id,
                'goods_attr_value' => $goods_attr_value,
            ])->delete();
    }

    public function getUserShop($user_id)
    {
    //  return Db::table('drh_shop')
    //         ->field([
    //             'id',
    //         ])->where([
    //             'user_id' => $user_id,
    //             'shop_type'=>2
    //         ])->find();
    return Db::table('drh_shop')
            ->field([
                'id',
            ])->where([
                'user_id' => $user_id
            ])->where('shop_type',"in","0,1")->find();
    }

    public function withdrawReview($order_num, $status, $msg, $time, $time_start)
    {
        $res = Db::table('drh_withdraw')
            ->where("order_num='{$order_num}' and create_time < {$time_start} and review_status=0")->update([
                'review_status' => $status,
                'msg' => $msg,
                'end_time' => $time
            ]);
        return $res == 1 ? $this->t_code : $this->f_code;
    }

    public function getWithdrawUserInfo($order_num)
    {
        return Db::table('drh_withdraw a')
            ->join('drh_user_info b', 'a.user_id=b.id', 'left')
            ->field([
                'b.user_integral',
                'b.user_money',
                'a.total_money',
                'a.user_id',
                'a.review_type'
            ])->where([
                'order_num' => $order_num
            ])->find();
    }

    public function showBargainOrder($user_id, $page, $pre, $sort)
    {

        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_user_goods_bargain a')
            ->join('drh_goods b', 'a.goods_id=b.id', 'left')
            ->field([
                'a.id',
                'b.goods_name',
                'b.goods_img',
                'b.goods_price',
                'a.bargain_total_money',
                '(a.bargain_total_num * a.bargain_once_money)  bargain_money',
                'a.bargain_total_money',
                'a.bargain_total_num',
                'a.bargain_num',
                'a.goods_attr'
            ])->where([
                'a.user_id' => $user_id
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    /**
     * 获取数量
     * @param $where
     * @param $table
     * @return mixed
     */
    public function total($where, $table = 'drh_shop')
    {
        $res = Db::table($table)
            ->field('count(*) count')
            ->where($where)
            ->find();

        return $res['count'];
    }

    public function addOriginalMember($user_id, $user_name, $card_number, $time, $img_positive, $img_back)
    {
        return Db::table('drh_original_member')
            ->insertGetId([
                'user_id' => $user_id,
                'user_name' => $user_name,
                'id_card' => $card_number,
                'img_positive' => $img_positive,
                'img_back' => $img_back,
                'create_time' => $time,
            ]);
    }

    public function showOriginalMember($page, $pre, $sort, $where)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);

        return Db::table('drh_original_member')
            ->field([
                'id',
                'user_id',
                'user_name',
                'id_card',
                'img_positive',
                'img_back',
                'verify_status',
                "FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') start_time",
                "FROM_UNIXTIME(end_time,'%Y-%m-%d %H:%i:%s') end_time",
            ])->where($where)
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function reviewOperating($id, $user_id, $status)
    {
        return Db::table('drh_original_member')
            ->where([
                'user_id' => $user_id,
                'id' => $id,
            ])->update([
                'verify_status' => $status
            ]);
    }

    public function addFriend($user_id, $friend_id, $time, $type)
    {
        return Db::table('drh_user_friend')
            ->insertGetId([
                'user_id' => $user_id,
                'friend_id' => $friend_id,
                'create_time' => $time,
                'type_status' => $type,
            ]);
    }

    public function showFriend($user_id, $page, $pre, $sort)
    {

        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_user_friend a')
            ->join('drh_user_info b', 'a.friend_id=b.id', 'left')
            ->join('drh_shop c', 'a.friend_id=c.user_id', 'left')
            ->join('drh_original_member d', 'a.friend_id=d.user_id', 'left')
            ->field([
                'b.id user_id',
                'b.user_name',
                'b.type_status',
                'b.user_head_img',
                'IFNULL(c.id,0) shop_id',//0没有开商户
                'IFNULL(d.user_id,0) original_id'//0 普通会员
            ])->where([
                'user_id' => $user_id
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    /**
     * 绑定手机号
     * @param $user_id
     * @param $mobile
     * @return int|string
     */
    public function bindingTel($user_id, $mobile)
    {
        return Db::table('drh_user_info')
            ->where([
                'id' => $user_id
            ])->update([
                'user_tel' => $mobile
            ]);
    }

    public function addUserTel($user_open_id, $user_tel)
    {
        return Db::table('drh_user_info')
            ->where([
                'user_open_id' => $user_open_id
            ])->update([
                'user_tel' => $user_tel
            ]);
    }

    public function attentionShop($user_id, $shop_id, $time)
    {
        return Db::table('drh_user_attention_shop')
            ->insertGetId([
                'user_id' => $user_id,
                'shop_id' => $shop_id,
                'recover_status' => 1,
                'create_time' => $time
            ]);
    }

    public function attentionShopCancel($user_id, $shop_id, $status)
    {
        return Db::table('drh_user_attention_shop')
            ->where([
                'user_id' => $user_id,
                'shop_id' => $shop_id,
            ])->update([
                'recover_status' => $status
            ]);
    }

    public function attentionGoodsCancel($user_id, $goods_id, $status)
    {
        return Db::table('drh_user_attention_goods')
            ->where([
                'user_id' => $user_id,
                'goods_id' => $goods_id,
            ])->update([
                'recover_status' => $status
            ]);
    }

    public function attentionGoods($user_id, $goods_id, $time)
    {
        return Db::table('drh_user_attention_goods')
            ->insertGetId([
                'user_id' => $user_id,
                'goods_id' => $goods_id,
                'recover_status' => 1,
                'create_time' => $time
            ]);
    }

    public function userCommentOrder($user_id, $order_num, $img, $msg, $integral, $time, $shop_id)
    {
        return Db::table('drh_user_comment')
            ->insertGetId([
                'user_id' => $user_id,
                'order_num' => $order_num,
                'shop_id' => $shop_id,
                'img' => $img,
                'msg' => $msg,
                'comment_integral' => $integral,
                'create_time' => $time,
            ]);
    }

    public function showCollectionGoods($where,$page,$pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_user_attention_goods a')
            ->join('drh_goods b','a.goods_id=b.id','left')
            ->field([
                'a.goods_id',
                'b.goods_name',
                'b.shop_id',
                'b.goods_price',
                'b.goods_attr_value',
                'b.goods_img',
                'b.is_shelves',
            ])->where($where)->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showCollectionShop($where,$page,$pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_user_attention_shop a')
            ->join('drh_shop b','a.shop_id=b.id','left')
            ->field([
                'a.shop_id',
                'b.shop_name',
                'b.shop_logo',
                'b.shop_introduce',
            ])->where($where)->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showUserGoodsBargain($where, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_user_goods_bargain a')
            ->join('drh_shop b','a.shop_id=b.id','left')
            ->join('drh_goods c','a.goods_id=c.id','left')
            ->field([
                'a.id bargain_id',
                'b.shop_name',
                'a.goods_id',
                'a.shop_id',
                'a.bargain_total_money',
                'a.bargain_once_money',
                'a.bargain_total_num',
                'a.bargain_num',
                'a.goods_attr',
                'c.goods_name'
            ])->where($where)->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function delGoodsOrder($user_id,$order_num)
    {
        return Db::table('drh_goods_order')
            ->where([
                'user_id'=>$user_id,
                'order_num'=>$order_num
            ])->update([
                'del_status'=>0
            ]);
    }

    public function delGoodsBargain($goods_attr,$goods_id,$shop_id,$user_id)
    {
        return Db::table('drh_user_goods_bargain')
            ->where([
                'goods_id'=>$goods_id,
                'shop_id'=>$shop_id,
                'user_id'=>$user_id,
                'goods_attr'=>$goods_attr,
            ])->delete();
    }

    public function delUserGoodsBargain($user_id, $bargain_id, $goods_id)
    {
        Db::table('drh_user_goods_bargain')
            ->where([
                'user_id' => $user_id,
                'goods_id' => $goods_id,
                'id' => $bargain_id,
            ])->delete();
    }
}